package cn.yougou.serviceImpl;

import cn.yougou.common.CalculateUtil;
import cn.yougou.common.DtoUtil;
import cn.yougou.dto.Dto;
import cn.yougou.mapper.YgCommodityProductMapper;
import cn.yougou.pojo.YgCommodityProduct;
import cn.yougou.service.shopcart.YgCommodityProductService;
import cn.yougou.vo.YgResCommodityVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * (YgCommodityProduct)表服务实现类
 *
 * @author 白杰崇
 * @since 2020-12-11 18:57:41
 */
@Service
public class YgCommodityProductServiceImpl implements YgCommodityProductService {

    @Autowired
    private YgCommodityProductMapper ygCommodityProductMapper;

    @Autowired
    private YgCommodityServiceImpl ygCommodityService;

    /**
     * 查询规格不同组合的商品(购物车/类型)
     * 计算不同规格的折扣价
     * @param ygCommodityProduct
     * @return
     */
    @Override
    public YgCommodityProduct getProduct(YgCommodityProduct ygCommodityProduct) {
        //1 获取规格组合
        YgCommodityProduct ygCp = ygCommodityProductMapper.queryAll(ygCommodityProduct);
        if(ygCp != null){
            //2 获取该商品折扣
            double discount = ygCommodityService.queryById(ygCommodityProduct.getCommodityid()).getDiscount();
            if(discount != -1){
                //3 计算出规格组合价格的折扣价
                double priceAft = CalculateUtil.getPrice(ygCp.getPrice(),discount);
                ygCp.setPrice(priceAft);
            }
            return ygCp;
        }
        return null;
    }

    /**
     * 下单成功服务 & 更改商品规格组合表的库存
     * 商品id  规格id  商品件数
     * @return
     */
    public boolean placeOrder(Integer commodityId,Integer productId,Integer number){
        //1 查询该规格是否已售空
        YgCommodityProduct ygCommodityProduct = new YgCommodityProduct();
        ygCommodityProduct.setCommodityid(commodityId);
        ygCommodityProduct.setId(productId);
        //获取该规格商品库存
        Integer stock = ygCommodityProductMapper.queryAll(ygCommodityProduct).getNumber();
        //库存大于0再执行减库存操作
        if(stock > 0){
            int config = ygCommodityProductMapper.placeOrder(commodityId,productId,number);
            if(config > 0){
                return true;
            }
        }else{ //库存为0直接返回false
            return false;
        }
        return false;
    }

    /**
     * 判断商品List中每一件商品是否有库存
     * @return
     */
    public Dto isItSoldOut(YgResCommodityVo[] ygResCommodityVos){
        //1 查询该规格是否已售空
        YgCommodityProduct ygCommodityProduct = new YgCommodityProduct();
        List<YgResCommodityVo> inventoryShortage = new ArrayList<>();
        for (int i = 0; i < ygResCommodityVos.length; i++) {
            ygCommodityProduct.setCommodityid(ygResCommodityVos[i].getId());
            ygCommodityProduct.setId(ygResCommodityVos[i].getProductid());
            //获取该规格商品库存
            Integer stock = ygCommodityProductMapper.queryAll(ygCommodityProduct).getNumber();
            //判断库存是否小于 该订单数量
            if(stock < ygResCommodityVos[i].getCommodityCount()){
                //将库存不足的商品添加进list
                inventoryShortage.add(ygResCommodityVos[i]);
            }
        }
        if(!inventoryShortage.isEmpty()){
            //库存不足，返回库存不足的商品
            return DtoUtil.returnSuccess("inventoryShortage",inventoryShortage);
        }else{
            //库存充足
            return DtoUtil.returnSuccess("adequateInventory",null);
        }

    }


    /**
     * 通过ID查询单条数据
     *
     * @param specificationId
     * @return 实例对象
     */
    @Override
    public YgCommodityProduct queryById(String specificationId) {
        return null;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<YgCommodityProduct> queryAllByLimit(int offset, int limit) {
        return this.ygCommodityProductMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param ygCommodityProduct 实例对象
     * @return 实例对象
     */
    @Override
    public YgCommodityProduct insert(YgCommodityProduct ygCommodityProduct) {
        this.ygCommodityProductMapper.insert(ygCommodityProduct);
        return ygCommodityProduct;
    }


    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ygCommodityProductMapper.deleteById(id) > 0;
    }
}